﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CognisessDA;
using System.Xml.Linq;

namespace CognisessWeb.Models
{
    public class TestRecordModel
    {
        public int Id { get; set; }
        public int TotalTime { get; set; }
        public int StepNo { get; set; }
        public string Value { get; set; }
        public int UserId { get; set; }
        public int TestId { get; set; }
        public long ResponseTime { get; set; }
        public static List<TestRecord> GetTestRecords(string strXML)
        {
            strXML = @"<data>
                      <userid>1</userid>
                      <testid>1</testid>
                      <infos>
                        <info isright='true' responsetime='2' bonus='400' totalscore='500'></info>
                        <info isright='true' responsetime='2' bonus='400' totalscore='500'></info>
                        <info isright='true' responsetime='2' bonus='400' totalscore='500'></info>
                        <info isright='true' responsetime='2' bonus='400' totalscore='500'></info>
                        <info isright='true' responsetime='2' bonus='400' totalscore='500'></info>
                        <info isright='true' responsetime='2' bonus='400' totalscore='500'></info>
                        <info isright='true' responsetime='2' bonus='400' totalscore='500'></info>
                        <info isright='true' responsetime='2' bonus='400' totalscore='500'></info>
                        <info isright='true' responsetime='2' bonus='400' totalscore='500'></info>
                        <info isright='true' responsetime='2' bonus='400' totalscore='500'></info>
                        <info isright='true' responsetime='2' bonus='400' totalscore='500'></info>
                        <info isright='true' responsetime='2' bonus='400' totalscore='500'></info>
                        <info isright='true' responsetime='2' bonus='400' totalscore='500'></info>
                        <info isright='true' responsetime='2' bonus='400' totalscore='500'></info>
                      </infos>
                      <totalscores>7000</totalscores>
                      <totaltimes>48</totaltimes>
                    </data>";

            //读取xml
            XElement doc = XElement.Parse(strXML);
            List<TestRecord> result = null;
            var infoList = doc.Descendants("infos").FirstOrDefault().Descendants().ToList();
            if (infoList != null)
            {
                for (int i = 0; i < infoList.Count(); i++)
                {
                    result.Add(new TestRecord
                    {
                        UserId = Convert.ToInt32(doc.Descendants("userid").FirstOrDefault().Value),
                        TestId = Convert.ToInt32(doc.Descendants("testid").FirstOrDefault().Value),
                        TotalTime = Convert.ToInt32(doc.Descendants("totaltimes").FirstOrDefault().Value),
                        Value = infoList[i].ToString()
                    });
                }
            }

            return result;
        }
    }
}
